////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                            //
//                                                         WNProject                                                          //
//                                                                                                                            //
//            This file is distributed under the BSD 2-Clause open source license. See License.txt for details.               //
//                                                                                                                            //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef __WN_CONCURRENCY_MUTEX_H__
#define __WN_CONCURRENCY_MUTEX_H__

#include "WNCore/inc/WNPlatform.h"
#include "WNCore/inc/WNTypes.h"
#include "WNCore/inc/WNMacros.h"

#ifdef _WN_WINDOWS
    #include <windows.h>
#elif defined _WN_LINUX
    #include <pthread.h>
#endif

namespace WNConcurrency {
    class WNMutex {
    public:
        WN_INLINE WNMutex();
        WN_INLINE ~WNMutex();

        WN_INLINE WN_VOID Lock();
        WN_INLINE WN_BOOL TryLock();
        WN_INLINE WN_VOID Unlock();

    private:
        #ifdef _WN_WINDOWS
            HANDLE mHandle;
        #elif defined _WN_LINUX
            pthread_mutex_t mMutex;
        #endif
    };

    class WNMutexScopeLock {
    public:
        WN_INLINE WNMutexScopeLock(WNMutex& _mutex);
        WN_INLINE ~WNMutexScopeLock();

    private:
        WNMutex& mMutex;

        WN_BOOL mLocked;
    };
}

#define WN_MUTEX_SCOPE_LOCK(_mutex) WNMutexScopeLock WN_UNIQUE_VARIABLE(mutex)(_mutex)

#include "WNConcurrency/inc/WNMutex.inl"

#endif // __WN_CONCURRENCY_MUTEX_H__